package com.cardbaobao.cardbabyclient.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.BaseResult;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Description:
 * Created by FHW
 * on 2017/11/15
 */

public class AddDepositCardResultActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.bt_set_repay_pwd)
    Button bt_set_repay_pwd;
    private int type = 2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_deposit_card_result;
    }

    @OnClick(R.id.bt_set_repay_pwd)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_set_repay_pwd:
                switch (type){
                    case 1:
                        break;
                    case 2:
                        Intent intent = new Intent();
                        intent.setClass(mContext, SetTradePwdActivity.class);
                        intent.putExtra("type", 0);
                        intent.putExtra(PRE_PAGE_ID_S,mCurrentPageId);
                        startActivityForResult(intent, Constant.REQUST_CODE);
                        break;
                }
                finish();
                break;
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mCurrentPageId= Self.Page.app_repayment_add_depositCard_success;
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        queryRepay();
    }

    /**
     * 查询是否设置支付密码
     */
    private void queryRepay() {
        showProgressDialog(false);
        Api.queryRepayPwd(String.valueOf(mApplication.getUser().getID()))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        dismissProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissProgressDialog();
                        ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                getString(R.string.string_request_data_fail));
                    }

                    @Override
                    public void onNext(String s) {
                        BaseResult baseResult = JsonUtil.json2Object(s, BaseResult.class);
                        if (baseResult.getCode() == Constant.SUC_CODE) {//已设置支付密码
                            type = 1;
                            bt_set_repay_pwd.setText("去还款");
                        } else if (baseResult.getCode() == 2) {//未设置支付密码
                            type = 2;
                            bt_set_repay_pwd.setText("设置支付密码");
                        } else {
                            ToastUtil.showToast(baseResult.getMsg());
                        }
                    }
                });
    }

}
